<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="index.php";}else{window.location.href="index.php";}</script>';
    } else {
        header('Access-Control-Allow-Origin: http://swissmooh.co.th');
        header('Access-Control-Allow-Origin: http://www.swissmooh.co.th');
    }
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>SwissMooh - Admin Page</title>

        <!-- Bootstrap Core CSS -->
        <link href="assets/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="assets/css/sb-admin.css" rel="stylesheet">

        <!-- Morris Charts CSS -->
        <link href="assets/css/plugins/morris.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="assets/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

    </head>

    <body>

        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="welcome_page.php">SwissMooh Admin</a>
                </div>
                <!-- Top Menu Items -->
                <ul class="nav navbar-right top-nav">                                        
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> <?= $_SESSION['username'] ?><b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="change_password.php">Change Password</a>
                            </li>
                            <li class="divider"></li>
                            <li>
                                <a href="#" id="logout_click"><i class="fa fa-fw fa-power-off"></i> Log Out</a>
                            </li>
                        </ul>
                    </li>
                </ul>
                <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
                <div class="collapse navbar-collapse navbar-ex1-collapse">
                    <ul class="nav navbar-nav side-nav">
                        <li>
                            <a href="menu_main_page.php"><i class="fa fa-fw fa-bar-chart-o"></i> Manage Menu List</a>
                        </li>
                        <li>
                            <a href="manage_carousel_page.php"><i class="fa fa-fw fa-edit"></i> Manage Carousel Content</a>
                        </li>
                        <li>
                            <a href="contact_main_page.php"><i class="fa fa-fw fa-edit"></i> Manage Contact Content</a>
                        </li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </nav>
            <div id="page-wrapper">

                <div class="container-fluid">

                    <!-- Page Heading -->
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">
                                Product <small>Manage product Page</small>
                            </h1>
                        </div>
                    </div>
                    <!-- /.row -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <h3 class="panel-title">Product Detail</h3>
                                </div>
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label>Product Name *</label>
                                        <input id="desc_name" class="form-control">
                                        <p class="help-block">Example: Edelweiss Cheese ... etc</p>
                                    </div>
                                    <div class="form-group">
                                        <label>Page Type *</label>
                                        <select id="page_type" class="form-control">
                                            <option value="0">---Please Select Page Type---</option>
                                            <option value="1">Normal Content</option>
                                            <option value="2">Images Content</option>
                                            <option value="3">Overview Content</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-6" id="uploadFileBox">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <div class="pull-right">
                                                    <input type="button" id="uploadfile" class="btn btn-xs btn-primary" value="Upload"/>
                                                </div>
                                                <h4 class="panel-title">Product File Attach</h4>                                   
                                            </div>
                                            <div class="panel-body">
                                                <div class="list-group" id="file_menu_list">            

                                                </div>
                                            </div>
                                        </div>
                                    </div> 
                                    <div class="col-lg-6" id="uploadImageBox">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <div class="pull-right">
                                                    <input type="button" id="uploadImage" class="btn btn-xs btn-primary" value="Upload"/>
                                                </div>
                                                <h3 class="panel-title">Product Image Attach</h3>
                                            </div>
                                            <div class="panel-body">
                                                <div class="list-group" id="image_menu_list">            

                                                </div>
                                            </div>
                                        </div>
                                    </div> 
                                </div>
                            </div>
                        </div>      
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <h3 class="panel-title">Product Description</h3>
                                </div>
                                <div class="panel-body">
                                    <textarea class="summernote" name="product_description" id="product_description"></textarea>
                                </div>
                            </div>
                        </div>
                        <button type="button" onclick="submitDescriptionPage()" class="btn btn-default">Submit</button>
                        <button type="button" onclick="cancelCreateDesc()" class="btn btn-default">Cancel</button>
                    </div>
                    <!-- /.row -->
                </div>
                <!-- /.container-fluid -->
                <div id="dialog_upload_file" title="Upload File to server...">
                    <div>
                        <ul class="list-group">
                            <li href="#" class="list-group-item active">
                                File Upload
                                <div class="pull-right">
                                    <input type="button" class="btn btn-xs btn-primary close_file_upload_dialog" value="Close"/>
                                </div>
                            </li>
                            <li class = "list-group-item">
                                <div class="mulitplefileuploaderFile">Upload</div>
                                <div id="status"></div>
                            </li>
                        </ul>
                    </div>
                    <div id="edit_image"></div>
                </div>
                <div id="dialog_upload_image" title="Upload Images to server...">
                    <div>
                        <ul class="list-group">
                            <li href="#" class="list-group-item active">
                                File Upload
                                <div class="pull-right">
                                    <input type="button" class="btn btn-xs btn-primary close_file_upload_dialog" value="Close"/>
                                </div>
                            </li>
                            <li class = "list-group-item">
                                <div class="mulitplefileuploaderImage">Upload</div>
                                <div id="status"></div>
                            </li>
                        </ul>
                    </div>
                    <div id="edit_image"></div>
                </div>
                <div id="dialog_show_link" title="Get Link">
                    <span id="show_link_span"></span>
                </div>
            </div>

        </div>
        <!-- /#wrapper -->
        <!-- Bootstrap Core CSS -->
        <link href="assets/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="assets/css/sb-admin.css" rel="stylesheet">
        <!-- Custom Fonts -->
        <link href="assets/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link href="assets/css/jquery-ui.min.css" rel="stylesheet" type="text/css">
        <link href="assets/css/plugins/summernote/summernote.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="assets/css/uploadfile.css" media="screen" />
        <!-- jQuery -->
        <script src="assets/js/jquery.js"></script>
        <script src="assets/js/jquery-ui.min.js"></script>
        <script type="text/javascript" src="assets/js/jquery.uploadfile.js"></script>
        <!-- Bootstrap Core JavaScript -->
        <script src="assets/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="assets/js/plugins/summernote/summernote.js"></script>
        <script src="assets/js/jquery.blockUI.js"></script>

        <link href="assets/css/jquery.fancybox.css" rel="stylesheet">
        <script type="text/javascript" src="assets/js/jquery.fancybox.js"></script>

        <script src="assets/js/SwissMoohScript.js"></script>

        <script type="text/javascript">
                            var settings, setting2;
                            var uploadFileDialog = $("#dialog_upload_file").dialog({
                                closeOnEscape: true,
                                autoOpen: false,
                                height: 400,
                                width: 500,
                                modal: true
                            });
                            var uploadImageDialog = $("#dialog_upload_image").dialog({
                                closeOnEscape: true,
                                autoOpen: false,
                                height: 400,
                                width: 500,
                                modal: true
                            });

                            $(document).ready(function () {
                                updateSessionTimeOutCallBack();
                                $("#page_type").change(function () {
                                    var pageType = $(this).val();
                                    if (pageType == 3 || pageType == 2) {
                                        $("#uploadFileBox").hide();
                                        $("#uploadImageBox").hide();
                                    } else {
                                        $("#uploadFileBox").show();
                                        $("#uploadImageBox").show();
                                    }
                                });
                                $(".fancies").fancybox();
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/GetNullFileContent.php",
                                    type: 'POST',
                                    success: function (data, textStatus, jqXHR) {
                                        $("#file_menu_list").html(data);
                                    }
                                });
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/GetNullImageContent.php",
                                    type: 'POST',
                                    success: function (data, textStatus, jqXHR) {
                                        $("#image_menu_list").html(data);
                                    }
                                });
                                settings = {
                                    url: "model/com.swiss.mooh.controller/upload_file_attach.php",
                                    dragDrop: true,
                                    fileName: "myfile",
                                    allowedTypes: "doc,pdf,zip,xls,xlsx",
                                    returnType: "json",
                                    onSuccess: function (files, data, xhr)
                                    {
                                        //alert("Upload file: " + data + " complete !!");
                                    },
                                    showDelete: true,
                                    deleteCallback: function (data, pd)
                                    {
                                        for (var i = 0; i < data.length; i++)
                                        {
                                            $.post("model/com.swiss.mooh.controller/delete_file_attach.php", {op: "delete", name: data[i]},
                                            function (resp, textStatus, jqXHR)
                                            {
                                                //alert(resp);
                                            });
                                        }
                                        pd.statusbar.hide(); //You choice to hide/not.

                                    }
                                };
                                setting2 = {
                                    url: "model/com.swiss.mooh.controller/upload_image_attach.php",
                                    dragDrop: true,
                                    fileName: "myfile",
                                    allowedTypes: "jpg,png,JPG,gif,PNG",
                                    returnType: "json",
                                    onSuccess: function (files, data, xhr)
                                    {
                                        //alert("Upload file: " + data + " complete !!");
                                    },
                                    showDelete: true,
                                    deleteCallback: function (data, pd)
                                    {
                                        for (var i = 0; i < data.length; i++)
                                        {
                                            $.post("model/com.swiss.mooh.controller/delete_image_attach.php", {op: "delete", name: data[i]},
                                            function (resp, textStatus, jqXHR)
                                            {
                                                //alert(resp);
                                            });
                                        }
                                        pd.statusbar.hide(); //You choice to hide/not.

                                    }
                                };
                                var uploadObj = $(".mulitplefileuploaderFile").uploadFile(settings);
                                var uploadObj2 = $(".mulitplefileuploaderImage").uploadFile(setting2);
                                var uiSummernote = function () {
                                    /* Extended summernote editor */
                                    if ($(".summernote").length > 0) {
                                        $(".summernote").summernote({
                                            height: 300,
                                            codemirror: {
                                                mode: 'text/html',
                                                htmlMode: true,
                                                lineNumbers: true,
                                                theme: 'default'
                                            }
                                        });
                                    }
                                };
                                uiSummernote();
                                $("#uploadImage").click(function () {
                                    updateSessionTimeOutCallBack();
                                    uploadImageDialog.dialog("open");
                                });
                                $("#uploadfile").click(function () {
                                    updateSessionTimeOutCallBack();
                                    uploadFileDialog.dialog("open");
                                });
                                $(".close_file_upload_dialog").click(function () {
                                    updateSessionTimeOutCallBack();
                                    uploadFileDialog.dialog("close");
                                    window.location = "main_description_page.php?menuIDSelect=<?= $_GET['menuIDSelect'] ?>";
                                });
                            });
                            function deleteFileByName(fileName) {
                                updateSessionTimeOutCallBack();
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/DeleteFileByName.php",
                                    type: 'POST',
                                    data: {'fileName': fileName},
                                    success: function (data, textStatus, jqXHR) {
                                        $("#file_menu_list").html(data);
                                    }
                                });
                            }
                            function deleteImageByName(imageName) {
                                updateSessionTimeOutCallBack();
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/DeleteImageByName.php",
                                    type: 'POST',
                                    data: {'imageName': imageName},
                                    success: function (data, textStatus, jqXHR) {
                                        $("#image_menu_list").html(data);
                                    }
                                });
                            }
                            function getLink(path) {
                                updateSessionTimeOutCallBack();
                                $("#dialog_show_link").dialog();
                                $("#show_link_span").html(path);
                            }
                            function cancelCreateDesc() {
                                updateSessionTimeOutCallBack();
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/DeleteAllFileTemp.php",
                                    type: 'POST',
                                    success: function (data, textStatus, jqXHR) {
                                        if (data == 200) {
                                            $.ajax({
                                                url: "model/com.swiss.mooh.controller/DeleteAllImageTemp.php",
                                                type: 'POST',
                                                success: function (data2, textStatus, jqXHR) {
                                                    window.location = "menu_main_page.php";
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                            function submitDescriptionPage() {
                                updateSessionTimeOutCallBack();
                                var descName = $("#desc_name").val();
                                var page_type = $("#page_type").val();
                                if (page_type == 2 || page_type == 3) {
                                    $.ajax({
                                        url: "model/com.swiss.mooh.controller/CreateAndUpdateDescriptionByID.php",
                                        type: 'POST',
                                        data: {'type': 0, 'page_type': page_type, 'menuSelectID': '<?= $_GET['menuIDSelect'] ?>', 'descName': $("#desc_name").val(), 'descDesc': $("#product_description").code()},
                                        beforeSend: function (xhr) {
                                            $.blockUI({css: {
                                                    border: 'none',
                                                    padding: '15px',
                                                    backgroundColor: '#fff',
                                                    '-webkit-border-radius': '10px',
                                                    '-moz-border-radius': '10px',
                                                    opacity: .5,
                                                    color: '#fff'
                                                }, message: '<img src="assets/images/gears.gif"/>'});
                                        },
                                        success: function (data, textStatus, jqXHR) {
                                            $.post("model/com.swiss.mooh.controller/UpdateFileToRelateDescription.php", {'descID': data}, function (resultFromServer) {
                                                if (resultFromServer == 200) {
                                                    $.post("model/com.swiss.mooh.controller/UpdateImageToRelateDescription.php", {'descID': data}, function (resultFromServer2) {
                                                        if (resultFromServer2) {
                                                            window.location = "menu_main_page.php";
                                                            setTimeout($.unblockUI, 100);
                                                        } else {
                                                            alert(resultFromServer2);
                                                        }
                                                    });
                                                } else {
                                                    alert(resultFromServer);
                                                    setTimeout($.unblockUI, 100);
                                                }

                                            });
                                        }
                                    });
                                } else {
                                    if (descName == "" || page_type == "0") {
                                        alert("Please enter product name and/or page type");
                                    } else {
                                        $.ajax({
                                            url: "model/com.swiss.mooh.controller/CreateAndUpdateDescriptionByID.php",
                                            type: 'POST',
                                            data: {'type': 0, 'page_type': page_type, 'menuSelectID': '<?= $_GET['menuIDSelect'] ?>', 'descName': $("#desc_name").val(), 'descDesc': $("#product_description").code()},
                                            beforeSend: function (xhr) {
                                                $.blockUI({css: {
                                                        border: 'none',
                                                        padding: '15px',
                                                        backgroundColor: '#fff',
                                                        '-webkit-border-radius': '10px',
                                                        '-moz-border-radius': '10px',
                                                        opacity: .5,
                                                        color: '#fff'
                                                    }, message: '<img src="assets/images/gears.gif"/>'});
                                            },
                                            success: function (data, textStatus, jqXHR) {
                                                $.post("model/com.swiss.mooh.controller/UpdateFileToRelateDescription.php", {'descID': data}, function (resultFromServer) {
                                                    if (resultFromServer == 200) {
                                                        $.post("model/com.swiss.mooh.controller/UpdateImageToRelateDescription.php", {'descID': data}, function (resultFromServer2) {
                                                            if (resultFromServer2) {
                                                                window.location = "menu_main_page.php";
                                                                setTimeout($.unblockUI, 100);
                                                            } else {
                                                                alert(resultFromServer2);
                                                            }
                                                        });
                                                    } else {
                                                        alert(resultFromServer);
                                                        setTimeout($.unblockUI, 100);
                                                    }

                                                });
                                            }
                                        });
                                    }
                                }

                            }
        </script>
    </body>

</html>
